package com.xiaoyu.lanling.feature.fate.activity

import android.os.Bundle
import com.xiaoyu.base.model.User.SEX_MAN
import com.xiaoyu.base.model.User.SEX_WOMAN
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.event.fate.MainFatePreferenceUpdateEvent
import com.xiaoyu.lanling.feature.user.model.UserExtra
import com.xiaoyu.lanling.feature.user.model.UserPreference
import com.xiaoyu.lanling.widget.picker.area.PickerAreaBottomSheetDialog
import kotlinx.android.synthetic.main.activity_fate_preference.*

class FatePreferenceActivity : AppCompatToolbarActivity() {

    private lateinit var locationPickerDialog: PickerAreaBottomSheetDialog
    private lateinit var verifyPreference: String
    private lateinit var sexPreference: String

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.activity_fate_preference)
        initToolbar()

        initView()
        initBind()
        initData()
    }

    private fun initView() {
        title = getString(R.string.fate_preference_title)

        age_range_bar.setCircleMinInterval(1F)
        age_range_bar.tickEnd = UserPreference.MAX_AGE.toFloat()
        age_range_bar.tickStart = UserPreference.MIN_AGE.toFloat()
        locationPickerDialog = PickerAreaBottomSheetDialog.create(this)
    }

    private fun initBind() {
        sex_man_text.setOnClickDebounceListener { updateSex(SEX_MAN) }
        sex_woman_text.setOnClickDebounceListener { updateSex(SEX_WOMAN) }
        verify_all_text.setOnClickListener { updateVerify(UserPreference.VERIFY_ALL) }
        verify_verified_text.setOnClickListener { updateVerify(UserPreference.VERIFY_VERIFIED) }
        age_range_bar.setOnRangeBarHandUpListener { _, _, _, _, _ -> }
        age_range_bar.setOnRangeBarChangeListener { _, _, _, minAge, maxAge ->
            updateAge(minAge, maxAge)
        }
        location_layout.setOnClickDebounceListener { locationPickerDialog.show() }
        locationPickerDialog.setOnDateResultListener { province, city, county ->
            location_province_text.text = province
            location_city_text.text = city
        }
        button.setOnClickDebounceListener {
            val minAge = age_range_bar.leftPinValue
            val maxAge = age_range_bar.rightPinValue

            val province = location_province_text.text.toString()
            val city = location_city_text.text.toString()

            UserExtra.instance.updatePreference(sexPreference, verifyPreference, minAge, maxAge, province, city)
            MainFatePreferenceUpdateEvent().post()
            finish()
        }
    }


    private fun initData() {
        val preference = UserExtra.instance.preference
        updateSex(preference.sex)
        updateVerify(preference.verify)
        updateAge(preference.minAge, preference.maxAge)
        age_range_bar.setRangePinsByValue(preference.minAge.toFloat(), preference.maxAge.toFloat())
        location_province_text.text = preference.province
        location_city_text.text = preference.city
    }

    private fun updateSex(sex: String) {
        sexPreference = sex
        sex_man_text.isSelected = sex == SEX_MAN
        sex_woman_text.isSelected = sex == SEX_WOMAN
    }

    private fun updateVerify(verify: String) {
        verifyPreference = verify
        verify_all_text.isSelected = verify == UserPreference.VERIFY_ALL
        verify_verified_text.isSelected = verify == UserPreference.VERIFY_VERIFIED
    }

    private fun updateAge(min: String, max: String) {
        age_text.text = getString(R.string.fate_preference_age_desc, min, max)
    }
}